package com.ChainBizPro.system.service.impl;

import com.ChainBizPro.system.common.ErrorCode;
import com.ChainBizPro.system.exception.BusinessException;
import com.ChainBizPro.system.mapper.StockMapper;
import com.ChainBizPro.system.mapper.StockTransactionsMapper;
import com.ChainBizPro.system.model.domain.StockTransactions;
import com.ChainBizPro.system.model.dto.stocktransactions.StockTransactionsQueryRequest;
import com.ChainBizPro.system.model.enums.TransactionTypeEnum;
import com.ChainBizPro.system.model.vo.StockTransactionsVO;
import com.ChainBizPro.system.service.StockTransactionsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liusijia
 * @description 针对表【stock_transactions(库存交易)】的数据库操作Service实现
 * @createDate 2024-11-16 16:34:17
 */
@Service
public class StockTransactionsServiceImpl extends ServiceImpl<StockTransactionsMapper, StockTransactions> implements StockTransactionsService {

    @Resource
    private StockTransactionsMapper stockTransactionsMapper;

    @Override
    public Page<StockTransactionsVO> getQueryWrapper(Page<StockTransactionsVO> page, StockTransactionsQueryRequest stockTransactionsQueryRequest) {
        // 自定义分页查询
        List<StockTransactionsVO> stockTransactionsVOS = stockTransactionsMapper.getQuery(page, stockTransactionsQueryRequest);
        if (stockTransactionsVOS == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询失败");
        }

        // 映射交易类型
        for (StockTransactionsVO vo : stockTransactionsVOS) {
            if ("in".equals(vo.getTransactionType())) {
                vo.setTransactionType(TransactionTypeEnum.IN.getValue());
            } else if ("out".equals(vo.getTransactionType())) {
                vo.setTransactionType(TransactionTypeEnum.OUT.getValue());
            }
        }

        // 设置分页结果
        page.setRecords(stockTransactionsVOS);
        return page;
    }

}




